package com.bjpowernode.crm.web.controller;

import com.bjpowernode.crm.pojo.Type;
import com.bjpowernode.crm.pojo.Value;
import com.bjpowernode.crm.service.ValueService;
import com.bjpowernode.crm.utils.UUIDUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("value")
public class ValueController {

    @Resource
    private ValueService valueService;


    //展示value首页数据
    @RequestMapping("index.html")
    public ModelAndView getValue(ModelAndView mv){
        List<Value> valueList =valueService.getValue();
        mv.addObject("valueList",valueList);
        mv.setViewName("settings/dictionary/value/index");

        return mv;
    }

    //添加功能：下拉选的动态展示
    @RequestMapping("save.html")
    public ModelAndView getOption(ModelAndView mv){
        List<Type> type = valueService.getOption();
        mv.addObject("type",type);
        mv.setViewName("/settings/dictionary/value/save");
        return mv;
    }

    //添加功能
    @RequestMapping("addValue.do")
    public String addValue(Value value){
        String uuid = UUIDUtil.getUUID();
        value.setId(uuid);
        valueService.addValue(value);
        return "redirect:/value/index.html";

    }

    //删除功能
    @RequestMapping("delValue.do")
    @ResponseBody
    public int delValue(String[] ids){

        int i =valueService.delValue(ids);
        return i;
    }


    //修改功能:数据回显
    @RequestMapping("editValue.do")
    public ModelAndView getEditValue(String id,ModelAndView mv){
        Value value =valueService.getEditValue(id);
        System.out.println(value);
        mv.addObject("value",value);
        mv.setViewName("/settings/dictionary/value/edit");
        return mv;
    }

    //修改功能：
    @RequestMapping("edit.do")
    public String editValue(Value value){
        valueService.editValue(value);
        return "redirect:/value/index.html";
    }

}
